package cc.fine.svc.main.service.impl;

import cc.fine.svc.config.redis.utils.RedisCache;
import cc.fine.svc.main.bean.TagWeight;
import cc.fine.svc.main.bean.es.VideoEsEntity;
import cc.fine.svc.main.bean.po.RelUserVideoHistory;
import cc.fine.svc.main.bean.po.TblVideo;
import cc.fine.svc.main.service.RelUserVideoHistoryService;
import cc.fine.svc.main.service.WeightService;
import cc.fine.svc.main.service.tbl.TblVideoService;
import cc.fine.svc.main.utils.EsUtil;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/11/3
 */
@Service
public class WeightServiceImpl implements WeightService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private EsUtil esUtil;

    @Autowired
    private TblVideoService videoService;

    @Override
    public void save(TagWeight weight) {
        Map<String, Long> map = redisCache.getCacheMapValue(Weight_Map_Cache, weight.getUserId());
        if (map == null) {
            map = new HashMap<>();
            map.put("0", 0L);
        }

        // 计算最大权重值
        Long max = map.values().stream().
                max(Long::compareTo)
                .orElse(100L);

        for (String tag : weight.getTags()) {
            if (map.containsKey(tag)) {
//                map.put(tag, map.get(tag) + weight.getWeight());
                map.put(tag, max);
            } else {
                map.put(tag, weight.getWeight());
            }
        }

        redisCache.setCacheMapValue(Weight_Map_Cache, weight.getUserId(), map);

    }

}
